package com.baofu.paycore.manager.storage.impl;

import com.baofu.paycore.dal.mapper.PreAuthMapper;
import com.baofu.paycore.dal.model.PreAuthDO;
import com.baofu.paycore.manager.storage.Storage;
import com.baofu.paycore.service.facade.enums.PayCoreErrorCode;
import com.system.commons.exception.BizServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 预授权主单存储
 *
 * <p/>
 *      1、新增订单
 *      2、更新订单
 *      3、废弃订单
 * </p>
 * User: xin deng  Date: 16/8/30 ProjectName: paycore Version: 1.0
 */
@Slf4j
@Repository
public class PreAuthManager implements Storage<PreAuthDO> {

    /**
     * 预授权底层Mapper
     */
    @Autowired
    private PreAuthMapper preAuthMapper;

    /**
     * 新增订单
     *
     * @param preAuthDO 订单信息
     */
    @Override
    public int addOrder(PreAuthDO preAuthDO) {
        return preAuthMapper.insert(preAuthDO);
    }

    /**
     * 更新订单
     *
     * @param preAuthDO 订单信息
     */
    @Override
    public int modifyOrder(PreAuthDO preAuthDO) {

        return preAuthMapper.update(preAuthDO);
    }

    /**
     * 废弃订单
     *
     * @param preAuthDO 订单信息
     */
    @Override
    public int deleteOrder(PreAuthDO preAuthDO) {
        return 0;
    }

    /**
     * 分页查询预授权信息
     *
     * @param preAuthDO     请求参数
     * @param beginPage     开始行
     * @param endPage       结束行
     * @return              预授权信息
     */
    public List<PreAuthDO> selectPreAuthPage(PreAuthDO preAuthDO, Integer beginPage, Integer endPage){

        return preAuthMapper.selectPreAuthPage(preAuthDO, beginPage, endPage);
    }

    /**
     * 查询预授权信息
     *
     * @param preAuthDO     请求参数
     * @return              总数
     */
    public int selectPreAuthCount(PreAuthDO preAuthDO){

        return preAuthMapper.selectPreAuthCount(preAuthDO);
    }
    /**
     * 根据订单号、支付流水号查询交易
     *
     * @return 交易
     */
    public PreAuthDO selectByOrderNoAndBizNo(String requestOrderNo, String requestBizNo, String requestSystem,
                                              Date requestDate){
        PreAuthDO preAuthDO = preAuthMapper.selectByOrderNoAndBizNo(
                requestOrderNo, requestBizNo, requestSystem, requestDate);
        if (preAuthDO == null) {
            log.error("查询结果集为空:{} {} {} {}", requestOrderNo, requestBizNo, requestSystem, requestDate);
            throw new BizServiceException(PayCoreErrorCode.EMPTY_QUERY_RESULT);
        }
        return preAuthDO;
    }
}
